package com.nineclock.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.common.threadlocal.CurrentUserHolder;
import com.nineclock.common.utils.BeanHelper;
import  com.nineclock.system.dto.SysDepartmentDTO;
import com.nineclock.system.mapper.SysCompanyUserMapper;
import com.nineclock.system.pojo.SysCompanyUser;
import com.nineclock.system.pojo.SysDepartment;
import com.nineclock.system.mapper.SysDepartmentMapper;
import com.nineclock.system.service.SysDepartmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class SysDepartmentServiceImpl implements SysDepartmentService {
    @Autowired
    SysDepartmentMapper departmentMapper;

    @Autowired
    SysCompanyUserMapper companyUserMapper;

    @Override
    public List<SysDepartmentDTO> queryDepartmentList() {
//        //查询部门列表
        List<SysDepartmentDTO> departmentDTOList = this.queryChildrenDepartmentList(0L);
//        return departmentDTOList;

        return departmentDTOList;
    }


    @Override
    public List<Long> queryDepartmentIds() {
        LambdaQueryWrapper<SysDepartment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysDepartment::getParentId,0);
        wrapper.eq(SysDepartment::getCompanyId,CurrentUserHolder.get().getCompanyId());
        List<SysDepartment> departmentList = departmentMapper.selectList(wrapper);
        if (CollectionUtil.isNotEmpty(departmentList)) {
            List<Long> ids = departmentList.stream().map(department -> {
                return department.getId();
            }).collect(Collectors.toList());
            return ids;
        }
        return new ArrayList<>();
    }



                    /*         移动端打卡-------获取考勤组     */

    @Override
    public List<Long> queryDepartmentsByUserId(Long companyUserId) {
        List<Long> ids = new ArrayList<>();
        if (companyUserId == null) {
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }
        //查询该员工所在的部门id
        SysCompanyUser companyUser = companyUserMapper.selectById(companyUserId);
        //获取当前企业部门的ID
        if (companyUser ==null) {
            throw new NcException(ResponseEnum.USER_NOT_FOUND);
        }

        //获取部门id
        Long departmentId = companyUser.getDepartmentId();
        ids.add(departmentId); //假如第一次传来的是10011  第二次是10010

        while (true) {

            //获取上级部门id
            SysDepartment department = departmentMapper.selectById(departmentId);
            Long parentId = department.getParentId();//查询10011的爹 10010  再查询10010的爹 是0

            if (parentId ==0 ) { //为0结束
                break;

            }
            ids.add(departmentId); // 添加到departmentId中 再循坏

        }

        return ids;
    }





    public List<SysDepartmentDTO> queryChildrenDepartmentList(Long parentId){
        LambdaQueryWrapper<SysDepartment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysDepartment::getParentId,parentId);
        wrapper.eq(SysDepartment::getCompanyId,CurrentUserHolder.get().getCompanyId());

        List<SysDepartment> departmentList = departmentMapper.selectList(wrapper);
        if (CollectionUtil.isNotEmpty(departmentList)){
            List<SysDepartmentDTO> departmentDTOList = BeanHelper.copyWithCollection(departmentList, SysDepartmentDTO.class);
            for (SysDepartmentDTO departmentDTO : departmentDTOList) {
                Long id = departmentDTO.getId();
                List<SysDepartmentDTO> departmentDTOS = this.queryChildrenDepartmentList(id);
                if (CollectionUtil.isNotEmpty(departmentDTOS)) {
                    departmentDTO.setChildren(departmentDTOS);
                }else {
                    departmentDTO.setChildren(new ArrayList<>());
                }
            }
            return departmentDTOList;
        }
        return new ArrayList<>();
    }
}

//    public List<SysDepartmentDTO> queryChildrenDepartmentList(Long patentId){//0
//        //0 : 查询以及部门   100009查询该部门的子部门
//        //查询一级部门
//        LambdaQueryWrapper<SysDepartment> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(SysDepartment::getParentId,0);
//        wrapper.eq(SysDepartment::getCompanyId, CurrentUserHolder.get().getCompanyId());//企业id
//
//        // 查询到部门列表
//        List<SysDepartment> departmentList = departmentMapper.selectList(wrapper); //查询
//
//        if (CollectionUtil.isNotEmpty(departmentList)) {
//            //把得到的部门列表转换成 DTO
//            List<SysDepartmentDTO> departmentDTOList = BeanHelper.copyWithCollection(departmentList, SysDepartmentDTO.class);
//            for (SysDepartmentDTO departmentDTO : departmentDTOList) {
//                Long id = departmentDTO.getId();
//                List<SysDepartmentDTO> departmentDTOS = this.queryChildrenDepartmentList(id);
//                if (CollectionUtil.isNotEmpty(departmentDTOS)) {
//                    departmentDTO.setChildren(departmentDTOS);
//                }else {
//                    departmentDTO.setChildren(new ArrayList<>());
//                }
//
//            }
//            return departmentDTOList;
//        }
//        return new ArrayList<>();
//
//
//    }

